//
//  YBClassProgressViewController.m
//  ZhongWangYuanApp
//
//  Created by apple on 18/6/3.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YBClassProgressViewController.h"
#import "YBCompleteProgressView.h"
#import "YBObjectIntroductionModel.h"
#import "YBClassProgressModel.h"
#import "YBUserExamModel.h"
#import "ZJWebViewController.h"


@interface YBClassProgressViewController ()
@property (nonatomic , strong) UILabel * mustStudyLabel;

@property (nonatomic , strong) UILabel * chooseStudyLabel;

@property (nonatomic , strong) YBCompleteProgressView * mustStudyProgressView;

@property (nonatomic , strong) YBCompleteProgressView * chooseStudyProgressView;

@property (nonatomic , strong) UIScrollView * mainScrollView;

@property (nonatomic , strong) UILabel * currentScoreLabel;

@property (nonatomic , strong) UILabel * maxScoreLabel;

@property (nonatomic , strong) UIButton * functionButton;

@property (nonatomic , strong) YBClassProgressModel * progressModel;

@property (nonatomic , strong) YBUserExamModel * maxExamModel;

@property (nonatomic , strong) YBUserExamModel * examModel;

@property (nonatomic , strong) UIView * topBgView;
@property (nonatomic , strong) UIView * progressBgView;
@property (nonatomic , strong) UIView * testResultBgView;

@property (nonatomic , assign) BOOL isFirstIn;


@end

@implementation YBClassProgressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.isFirstIn = YES;
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"currentIsSign"]) {
        ((YBObjectIntroductionModel *)self.model).issignin = @"1";
    }
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_objectClassLearnerIsSignIn) name:@"ObjectClassLearnerIsSignIn" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_objectClassLearnerIsSignIn) name:@"ObjectClassDetailIsSignIn" object:nil];
    [self initUserInterface];
    [self addProgressView];
    [self addTestResultView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initDataSource];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)option_objectClassLearnerIsSignIn{
    ((YBObjectIntroductionModel *)self.model).issignin = @"1";
    [self initDataSource];
}

-(void)initDataSource{
    if (self.isFirstIn) {
        [YJProgressHUD showMessage:@"加载中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
        self.isFirstIn = NO;
    }
    [YBRequestManager request_objectGetClassProgressWithIdString:((YBObjectIntroductionModel *)self.model).ID successBlock:^(id object) {
        [YJProgressHUD hide];
        if ([object[@"code"] integerValue] == 1) {
            if ([object[@"data"] isKindOfClass:[NSArray class]]) {
                self.progressModel = [YBClassProgressModel yy_modelWithJSON:object[@"data"][0]];
                
                if (((YBObjectIntroductionModel *)self.model).issignin) {
                    if ([self.progressModel.isexam integerValue] == 1) {
                        self.topBgView.hidden = NO;
                        self.progressBgView.hidden = NO;
                        self.testResultBgView.hidden = NO;
                        [self getExamInfo];
                    }
                    else{
                        self.topBgView.hidden = NO;
                        self.progressBgView.hidden = NO;
                        self.testResultBgView.hidden = YES;
                    }
                    [self loadTopBgViewDataSource];
                    [self loadmiddleBgViewDataSource];
                }
                else{
                    self.topBgView.hidden = NO;
                    self.progressBgView.hidden = YES;
                    self.testResultBgView.hidden = YES;
                    [self loadTopBgViewDataSource];
                }
                [self reloadScrollViewContentSize];
                [self loadFunctionButtonShow];
                
                
            }
        }
        else{
            [YJProgressHUD showMsgWithoutView:@"获取失败"];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"获取失败"];
    }];
}

-(void)getExamInfo{
    [YBRequestManager request_objectGetExamInfoWithIdString:((YBObjectIntroductionModel *)self.model).ID successBlock:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            NSArray * tempArray = object[@"data"][@"userexam"];
            if (tempArray.count > 0) {
                self.maxExamModel = [YBUserExamModel yy_modelWithJSON:tempArray[0]];
                self.maxScoreLabel.text = [NSString stringWithFormat:@"%@", self.maxExamModel.maxexamscore];
            }
            
        }
        else{
//            [YJProgressHUD showMsgWithoutView:@"获取失败"];
        }
    } failBlock:^(id object) {
//        [YJProgressHUD showMsgWithoutView:@"获取失败"];
    }];
    
    [YBRequestManager request_objectGetCurrentExamInfoWithIdString:((YBObjectIntroductionModel *)self.model).ID successBlock:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            NSArray * tempArray = object[@"data"][@"userexam"];
            if (tempArray.count > 0) {
                self.examModel = [YBUserExamModel yy_modelWithJSON:tempArray[0]];
                self.currentScoreLabel.text = [NSString stringWithFormat:@"%@", self.examModel.examscore];
            }
            
        }
        else{
//            [YJProgressHUD showMsgWithoutView:@"获取失败"];
        }
    } failBlock:^(id object) {
//        [YJProgressHUD showMsgWithoutView:@"获取失败"];
    }];
}







-(void)reloadScrollViewContentSize{
    if (self.progressBgView.hidden && self.testResultBgView.hidden) {
        self.mainScrollView.contentSize = CGSizeMake([UIScreen zj_screenWith], self.chooseStudyLabel.bottom + ItemHeight * 110);
        if (self.mainScrollView.contentSize.height > self.mainScrollView.bounds.size.height) {
            self.functionButton.sd_layout
            .leftSpaceToView(self.mainScrollView , 0)
            .rightSpaceToView(self.mainScrollView , 0)
            .topSpaceToView(self.chooseStudyLabel , ItemHeight * 20)
            .heightIs(ItemHeight * 90);
        }
        else{
            self.functionButton.sd_layout
            .leftSpaceToView(self.mainScrollView , 0)
            .rightSpaceToView(self.mainScrollView , 0)
            .bottomSpaceToView(self.mainScrollView , 0)
            .heightIs(ItemHeight * 90);
        }
        [self.functionButton updateLayout];
    }
    if (!self.progressBgView.hidden && self.testResultBgView.hidden){
        self.mainScrollView.contentSize = CGSizeMake([UIScreen zj_screenWith], self.progressBgView.bottom + ItemHeight * 110);
        if (self.mainScrollView.contentSize.height > self.mainScrollView.bounds.size.height) {
            self.functionButton.sd_layout
            .leftSpaceToView(self.mainScrollView , 0)
            .rightSpaceToView(self.mainScrollView , 0)
            .topSpaceToView(self.progressBgView , ItemHeight * 20)
            .heightIs(ItemHeight * 90);
        }
        else{
            self.functionButton.sd_layout
            .leftSpaceToView(self.mainScrollView , 0)
            .rightSpaceToView(self.mainScrollView , 0)
            .bottomSpaceToView(self.mainScrollView , 0)
            .heightIs(ItemHeight * 90);
        }
        [self.functionButton updateLayout];
    }
    if (!self.progressBgView.hidden && !self.testResultBgView.hidden){
        self.mainScrollView.contentSize = CGSizeMake([UIScreen zj_screenWith], self.testResultBgView.bottom + ItemHeight * 110);
        if (self.mainScrollView.contentSize.height > self.mainScrollView.bounds.size.height) {
            self.functionButton.sd_layout
            .leftSpaceToView(self.mainScrollView , 0)
            .rightSpaceToView(self.mainScrollView , 0)
            .topSpaceToView(self.testResultBgView , ItemHeight * 20)
            .heightIs(ItemHeight * 90);
        }
        else{
            self.functionButton.sd_layout
            .leftSpaceToView(self.mainScrollView , 0)
            .rightSpaceToView(self.mainScrollView , 0)
            .bottomSpaceToView(self.mainScrollView , 0)
            .heightIs(ItemHeight * 90);
        }
        [self.functionButton updateLayout];
    }
}


-(void)loadTopBgViewDataSource{
    self.mustStudyLabel.text = [NSString stringWithFormat:@"必修课共%@门，需修满%@门；" , self.progressModel.requirecount , self.progressModel.requiredpassmark];
    self.chooseStudyLabel.text = [NSString stringWithFormat:@"选修课共%@门，需修满%@门。" , self.progressModel.electivecount , self.progressModel.electivepassmark];
}

-(void)loadmiddleBgViewDataSource{
    [self.mustStudyProgressView setProgressWithTotal:[self.progressModel.requiredpassmark integerValue] completeNumber:[self.progressModel.cu_compulsorynumber integerValue]];
    [self.chooseStudyProgressView setProgressWithTotal:[self.progressModel.electivepassmark integerValue] completeNumber:[self.progressModel.cu_electivenumber integerValue]];
}


-(void)loadFunctionButtonShow{
    if (((YBObjectIntroductionModel *)self.model).issignin) {
        if ([self.progressModel.isexam integerValue] == 1) {
            if ([self.progressModel.examcount integerValue] > 0) {
                self.functionButton.hidden = NO;
                [self.functionButton setTitle:@"再考一次" forState:UIControlStateNormal];
                self.functionButton.enabled = YES;
                //                [self.functionButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
            }
            else{
                self.functionButton.hidden = NO;
                [self.functionButton setTitle:@"考试" forState:UIControlStateNormal];
                self.functionButton.enabled = YES;
                //                [self.functionButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
            }
//            if (self.progressModel.cu_compulsorynumber >= self.progressModel.requiredpassmark && self.progressModel.cu_electivenumber >= self.progressModel.electivepassmark) {
//                
//            }
//            else{
//                if ([self.progressModel.examcount integerValue] > 0) {
//                    self.functionButton.hidden = NO;
//                    self.functionButton.backgroundColor = [UIColor lightGrayColor];
//                    [self.functionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//                    [self.functionButton setTitle:@"再考一次" forState:UIControlStateNormal];
//                    self.functionButton.enabled = NO;
//                }
//                else{
//                    self.functionButton.hidden = NO;
//                    self.functionButton.backgroundColor = [UIColor lightGrayColor];
//                    [self.functionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//                    [self.functionButton setTitle:@"考试" forState:UIControlStateNormal];
//                    self.functionButton.enabled = NO;
//                }
//            }
        }
        else{
            self.functionButton.hidden = YES;
        }
    }
    else{
        self.functionButton.hidden = NO;
        [self.functionButton setTitle:@"报名" forState:UIControlStateNormal];
        self.functionButton.enabled = YES;
//        [self.functionButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    }
}


-(void)initUserInterface{
    [self.view addSubview:self.mainScrollView];
    self.mainScrollView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.view , 0)
    .bottomSpaceToView(self.view , 0);
    
    UIView * topBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 5)];
    topBackView.backgroundColor = [UIColor themeGrayColor];
    [self.mainScrollView addSubview:topBackView];
    
    self.topBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 5, [UIScreen zj_screenWith], ItemHeight * 174)];
    [self.mainScrollView addSubview:self.topBgView];
    
    UILabel * titleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 20, 5, [UIScreen zj_screenWith] - ItemWidth * 40, ItemHeight * 74) font:15 textColor:[UIColor textGrayColor] text:@"结业要求"];
    titleLabel.font = LYPBOLDFONT(15);
    [self.topBgView addSubview:titleLabel];
    
    [self.topBgView addSubview:self.mustStudyLabel];
    [self.topBgView addSubview:self.chooseStudyLabel];
    self.topBgView.hidden = YES;
    
    [self.mainScrollView addSubview:self.functionButton];
    self.functionButton.sd_layout
    .topSpaceToView(self.topBgView,0)
    .leftSpaceToView(self.mainScrollView , 0)
    .rightSpaceToView(self.mainScrollView , 0)
    .heightIs(ItemHeight * 90);
    
//    [self.mainScrollView setupAutoContentSizeWithBottomView:self.functionButton bottomMargin:0];
    
}

-(void)addProgressView{
    self.progressBgView = [[UIView alloc]initWithFrame:CGRectMake(0, ItemHeight * 174, [UIScreen zj_screenWith], ItemHeight * 220)];
    [self.progressBgView addSubview:self.mustStudyProgressView];
    [self.progressBgView addSubview:self.chooseStudyProgressView];
    [self.mainScrollView addSubview:self.progressBgView];
    self.progressBgView.hidden = YES;
    
    
}


-(void)addTestResultView{
    self.testResultBgView = [[UIView alloc]initWithFrame:CGRectMake(0, ItemHeight * 394, [UIScreen zj_screenWith], ItemHeight * 215 + 10)];
    [self.mainScrollView addSubview:self.testResultBgView];
    
    UIView * topTestResultView = [[UIView alloc]initWithFrame:CGRectMake(0, ItemHeight * 45, [UIScreen zj_screenWith], 5)];
    topTestResultView.backgroundColor = [UIColor themeGrayColor];
    [self.testResultBgView addSubview:topTestResultView];
    
    UILabel * testResultTitleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 20, ItemHeight * 95 + 5, ItemWidth * 80, ItemHeight * 70) font:15 textColor:[UIColor textGrayColor] text:@"考试"];
    testResultTitleLabel.font = LYPBOLDFONT(15);
    [self.testResultBgView addSubview:testResultTitleLabel];
    
    self.currentScoreLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 100, ItemHeight * 69 + 5, [UIScreen zj_screenWith] * 0.5 - ItemWidth * 100, ItemHeight * 70) font:35 textColor:[UIColor zj_colorWithHexString:@"f04243"] text:@"0"];
    self.currentScoreLabel.textAlignment = NSTextAlignmentCenter;
    self.currentScoreLabel.font = [UIFont systemFontOfSize:35 * [UIScreen zj_textFontScale] weight:UIFontWeightRegular];
    [self.testResultBgView addSubview:self.currentScoreLabel];
    
    UILabel * currentScoreTitleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 100, self.currentScoreLabel.bottom, [UIScreen zj_screenWith] * 0.5 - ItemWidth * 100, ItemHeight * 52) font:15 textColor:[UIColor zj_colorWithHexString:@"f04243"] text:@"本次得分"];
    currentScoreTitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.testResultBgView addSubview:currentScoreTitleLabel];
    
    self.maxScoreLabel = [ToolClass creatLabelWithFrame:CGRectMake([UIScreen zj_screenWith] * 0.5, ItemHeight * 69 + 5, [UIScreen zj_screenWith] * 0.5, ItemHeight * 70) font:35 textColor:[UIColor textGrayColor] text:@"0"];
    self.maxScoreLabel.textAlignment = NSTextAlignmentCenter;
    self.maxScoreLabel.font = [UIFont systemFontOfSize:35 * [UIScreen zj_textFontScale] weight:UIFontWeightRegular];
    [self.testResultBgView addSubview:self.maxScoreLabel];
    
    UILabel * maxScoreTitleLabel = [ToolClass creatLabelWithFrame:CGRectMake([UIScreen zj_screenWith] * 0.5, self.currentScoreLabel.bottom, [UIScreen zj_screenWith] * 0.5, ItemHeight * 52) font:15 textColor:[UIColor textGrayColor] text:@"最高得分"];
    maxScoreTitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.testResultBgView addSubview:maxScoreTitleLabel];
    
    UIView * middleLineView = [[UIView alloc]initWithFrame:CGRectMake([UIScreen zj_screenWith] * 0.5, ItemHeight * 75 + 5, 1, ItemHeight * 110)];
    middleLineView.backgroundColor = [UIColor themeGrayColor];
    [self.testResultBgView addSubview:middleLineView];
    
//    UIView * bottomTestResultView = [[UIView alloc]initWithFrame:CGRectMake(0, ItemHeight * 215 + 5, [UIScreen zj_screenWith], 5)];
//    bottomTestResultView.backgroundColor = [UIColor themeGrayColor];
//    [self.testResultBgView addSubview:bottomTestResultView];
    self.testResultBgView.hidden = YES;

    
}

-(void)option_functionButton:(UIButton *)sender{
    if (((YBObjectIntroductionModel *)self.model).issignin) {
        if ([self.progressModel.isexam integerValue] == 1) {
//            if (self.progressModel.cu_compulsorynumber >= self.progressModel.requiredpassmark && self.progressModel.cu_electivenumber >= self.progressModel.electivepassmark) {
//                if ([self.progressModel.examcount integerValue] > 0) {
//                    //跳转web
//                }
//                else{
//                    //跳转web
//
//                }
//                ZJWebViewController * zj = [[ZJWebViewController alloc]init];
//                zj.titleString = @"考试";
//                zj.isHiddenNav = YES;
//                zj.urlString = [NSString stringWithFormat:@"%@localStorage?classid=%@&userid=%@&studentid=%@" , EXAMURL,  ((YBObjectIntroductionModel *)self.model).ID, YBUSERINFOMODEL.userid , YBUSERINFOMODEL.studentId];
//                [self.navigationController pushViewController:zj animated:YES];
//            }
//            else{
//
//            }
            ZJWebViewController * zj = [[ZJWebViewController alloc]init];
            zj.titleString = @"考试";
            zj.isHiddenNav = YES;
            zj.urlString = [NSString stringWithFormat:@"%@html/indexfront.html#/goexam/%@/%@/%@/" , HTMLURL,  ((YBObjectIntroductionModel *)self.model).ID, YBUSERINFOMODEL.userid , YBUSERINFOMODEL.studentId];
            [self.navigationController pushViewController:zj animated:YES];
        }
    }
    else{
        [YJProgressHUD show:@"报名中..." inView:[UIApplication sharedApplication].keyWindow mode:YJProgressModeLoading];
        [YBRequestManager request_objectSignInWithIdString:((YBObjectIntroductionModel *)self.model).ID successBlock:^(id object) {
            [YJProgressHUD hide];
            if ([object[@"code"] integerValue] == 1) {
                if ([object[@"data"][@"result"] integerValue] == 1) {
                    [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"currentIsSign"];
                    [YJProgressHUD showMsgWithoutView:@"报名成功"];
                    ((YBObjectIntroductionModel *)self.model).issignin = @"1";
                    [self initDataSource];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"ObjectClassProsessIsSignIn" object:nil];
                }
                else{
                    [YJProgressHUD showMsgWithoutView:[NSString stringWithFormat:@"%@",object[@"data"][@"message"]]];
                }
            }
            else{
                [YJProgressHUD showMsgWithoutView:@"报名失败"];
            }
        } failBlock:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"报名失败"];
        }];
    }
}


-(UILabel *)mustStudyLabel
{
    if (!_mustStudyLabel) {
        _mustStudyLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 20, 5 + ItemHeight * 74, [UIScreen zj_screenWith] - ItemWidth * 40, ItemHeight * 50) font:14 textColor:[UIColor textGrayColor] text:@"必修课共0门，需修满0门；"];
    }
    return _mustStudyLabel;
}


-(UILabel *)chooseStudyLabel
{
    if (!_chooseStudyLabel) {
        _chooseStudyLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 20, 5 + ItemHeight * 124, [UIScreen zj_screenWith] - ItemWidth * 40, ItemHeight * 50) font:14 textColor:[UIColor textGrayColor] text:@"选修课共0门，需修满0门。"];
    }
    return _chooseStudyLabel;
}

-(YBCompleteProgressView *)mustStudyProgressView
{
    if (!_mustStudyProgressView) {
        _mustStudyProgressView = [[YBCompleteProgressView alloc]initWithFrame:CGRectMake(0, ItemHeight * 15, [UIScreen zj_screenWith], ItemHeight * 95) title:@"必修课"];
    }
    return _mustStudyProgressView;
}

-(YBCompleteProgressView *)chooseStudyProgressView
{
    if (!_chooseStudyProgressView) {
        _chooseStudyProgressView = [[YBCompleteProgressView alloc]initWithFrame:CGRectMake(0, ItemHeight * 125, [UIScreen zj_screenWith], ItemHeight * 95) title:@"选修课"];
    }
    return _chooseStudyProgressView;
}


-(UIScrollView *)mainScrollView
{
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
    }
    return _mainScrollView;
}

-(UIButton *)functionButton
{
    if (!_functionButton) {
        _functionButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0, 1, 1) text:@"报名" textColor:[UIColor whiteColor] imageName:@"" font:16];
        _functionButton.hidden = YES;
        _functionButton.backgroundColor = [UIColor themeColor];
//        [_functionButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        [_functionButton addTarget:self action:@selector(option_functionButton:) forControlEvents:UIControlEventTouchUpInside];
        UIView * shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, -5, [UIScreen zj_screenWith], 5)];
        shadowView.userInteractionEnabled = NO;
        shadowView.backgroundColor = [UIColor themeGrayColor];
        [_functionButton addSubview:shadowView];
    }
    return _functionButton;
}

@end
